package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.MenuEntity;
import com.zhenwei.zscm.mapper.IMenuMapper;
import com.zhenwei.zscm.mappings.MenuMapping;
import com.zhenwei.zscm.model.dto.menu.FullMenuDTO;
import com.zhenwei.zscm.model.dto.menu.MenuDTO;
import com.zhenwei.zscm.model.vo.menu.MenuVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuRepo extends ServiceImpl<IMenuMapper, MenuEntity> {

    public List<MenuDTO> all() {
        var q = new LambdaQueryWrapper<MenuEntity>()
                .eq(MenuEntity::getDeleted, false)
                .orderByAsc(MenuEntity::getGroup)
                .orderByAsc(MenuEntity::getOrderBy);
        List<MenuEntity> list = this.list(q);
        return Linq.mapList(list, MenuMapping.INSTANCE::entity2Dto);
    }
    public List<FullMenuDTO> allV2() {
        var q = new LambdaQueryWrapper<MenuEntity>()
                .eq(MenuEntity::getDeleted, false)
                .orderByAsc(MenuEntity::getGroup)
                .orderByAsc(MenuEntity::getOrderBy);
        List<MenuEntity> list = this.list(q);
        return Linq.mapList(list, MenuMapping.INSTANCE::entity2FullDto);
    }


    public boolean create(MenuVO menu){
        MenuEntity e = MenuMapping.INSTANCE.vo2Entity(menu);
        e.setGuid(IdUtil.simpleUUID());
        return this.save(e);
    }
}
